﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Circle.Web.Application.Api;
using Circle.Web.ViewModelsClient;
using Circle.Web.Drivers.Contract;
using Circle.Domain.Model;
using Circle.Manager;
using System.Web.Mvc;
using Circle.Domain.Interface.Service;
using Microsoft.Practices.Unity;

namespace Circle.Web.Controllers.Api
{

    public class TopicController : BaseApiController
    {
        //[Dependency]
        private readonly ITopicDriver _topicDriver;

        public TopicController()
        {
            _topicDriver = DependencyResolver.Current.GetService<ITopicDriver>();
        }

        [System.Web.Http.HttpPost]
        public string TopicsByFilter([FromBody]string jsonModel)
        {
            var fitler = ClientModel.FromJson<PageTopicFilterClient>(jsonModel);
            var pageTopic = _topicDriver.PagingClientTopics(DefaultSetting.TopicsPerPage, fitler);

            return pageTopic.ToJson();
        }

        [System.Web.Http.HttpGet]
        public string Show(Guid key, int? pageIndex)
        {
            int pIndex = pageIndex.HasValue && pageIndex.Value > 0 ? pageIndex.Value : 0;
            var pageComment = _topicDriver.GetPageComments(key, pIndex, DefaultSetting.TopicsPerPage);
            return pageComment.ToJson();
        }

        
        private void SaveTopic(TopicClientModel topic)
        {
             _topicDriver.Save(topic);
        }

        [System.Web.Http.HttpPost]
        public string Create([FromBody]string jsonModel)
        {
            var topic = ClientModel.FromJson<TopicClientModel>(jsonModel);
            SaveTopic(topic);
            return topic.ToJson();
        }

        [System.Web.Http.HttpPut]
        public string Update(string key, [FromBody]string jsonModel)
        {
            var topic = ClientModel.FromJson<TopicClientModel>(jsonModel);
            SaveTopic(topic);
            return topic.ToJson();
        }

        [System.Web.Http.HttpDelete]
        public void Delete(string key)
        {
        }

    }
}
